package com.jeeplus.common.utils;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;

public class CacheUtils {

	private static CacheManager cacheManager = SpringContextHolder.getBean("shiroCacheManager", CacheManager.class);

	private static final String SYS_CACHE = "sysCache";

	public static Object get(String key) {
		return get(SYS_CACHE, key);
	}

	public static Object get(String cacheName, String key) {
		return getCache(cacheName).get(key);
	}

	public static void put(String key, Object value) {
		put(SYS_CACHE, key, value);
	}

	public static void put(String cacheName, String key, Object value) {
		getCache(cacheName).put(key, value);
	}

	public static void remove(String key) {
		remove(SYS_CACHE, key);
	}

	public static void remove(String cacheName, String key) {
		getCache(cacheName).remove(key);
	}

	private static Cache<String, Object> getCache(String cacheName) {
		if (cacheManager == null) {
			throw new IllegalStateException("cacheManager 未初始化，请确保 Spring 成功注入 CacheManager");
		}
		Cache<String, Object> cache = cacheManager.getCache(cacheName);
		if (cache == null) {
			throw new IllegalArgumentException("未找到名为 " + cacheName + " 的缓存！");
		}
		return cache;
	}
}
